package com.why.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;

import java.util.HashMap;
import java.util.Random;

public class SendSmsUtil {

    public static HashMap<String, Object> fasong(String cellphone) throws TencentCloudSDKException {
        String  appid = "1400517336"; //你创建的应用AppID
        String phone = "86"+cellphone; //手机号码,注意如果是国内的手机号码需要加86不然会报错
        int templated = 955072; //你的正文模板ID
        String smsSign = "您正在修改密码，验证码为：{1}，5分钟有效，为保障帐户安全，请勿向任何人提供此验证码。"; //你的签名模板中的内容,不是签名ID
        final String url = "https://yun.tim.qq.com/v3/tlssmssvr/sendsms"; //请求地址
        // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey，
        Credential cred = new Credential("AKIDRgfPt74lCMGtEI5bqpXy82f191dy2oKZ", "zjDQM1jALFsEK9ZmiOq2xc07yYsvhCRP");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setSignMethod(ClientProfile.SIGN_TC3_256);
        SmsClient client = new SmsClient(cred, "", clientProfile);


        SendSmsRequest req = new SendSmsRequest();
        req.setSmsSdkAppid(appid);  //appId
        String[] phoneNumberSet1 = {phone};//手机号
        req.setPhoneNumberSet(phoneNumberSet1);
        req.setTemplateID(templated+"");  //模版id

        String str="0123456789";
        StringBuilder sb=new StringBuilder(6);
        for(int i=0;i<6;i++)
        {
            char ch=str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        System.out.println(sb.toString());
        String [] templateParam={sb.toString()};//模版参数，从前往后对应的是模版的{1}、{2},可以传多个用逗号隔开
        req.setTemplateParamSet(templateParam);


        req.setSign(smsSign); //模板内容，不是填模板id
        req.setSmsSdkAppid("1400517336");//应用的SDK AppID
        req.setSign("whylienen");//签名内容


        SendSmsResponse resp = client.SendSms(req);
        System.out.println(SendSmsResponse.toJsonString(resp));

        HashMap<String, Object> map = new HashMap<>();
        map.put("code",sb.toString());
        map.put("sms",SendSmsResponse.toJsonString(resp));
        return map;
    }
}
